﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyServer.Common
{
    /// <summary>
    /// 表示数据交换中心基类
    /// </summary>
    public abstract class DataCenterBase
    {

        #region==========属性==========

        /// <summary>
        /// 表示数据中心装载的数据读取器
        /// </summary>
        public DataReaderBase Reader { get; protected set; }
        /// <summary>
        /// 表示数据中心装载的数据发送器
        /// </summary>
        public DataSenderBase Sender { get; protected set; }
        /// <summary>
        /// 表示数据中心装载的数据操作器
        /// </summary>
        public DataOperatorBase Operator { get; protected set; }
        /// <summary>
        /// 表示连接监听管理器实例
        /// </summary>
        public ConnectManagerBase ConnectionManager { get; protected set; }
        /// <summary>
        /// 表示数据中心装载的数据侦听器
        /// </summary>
        public DataListenerBase Listener { get; protected set; }
        #endregion

        #region=======可重写方法=======


        protected virtual void ConnectionManager_NewConnectionJoin(object sender, ref ConnectEventArgs e)
        {
            Listener.AddConnect(e.Connect);
            e.Canceled = false;
        }

        protected virtual void DataListener_DataAvailable(object sender, ref DataListenerEventArgs e)
        {
            lock (Reader.ReadQueue)
            {
                Reader.ReadQueue.Enqueue(e.Data);
                e.IsCancel = false;
            }
        }

        protected virtual void Operator_AfterOperateData(object sender, ref DataAfterOperateEventArgs e)
        {
            if (e.IsSucceed)
            {
                DataPackageCollection col = e.ResultData;
                if (col != null)
                {
                    Sender.SendQueue.Enqueue(col);
                }
            }
        }

        protected virtual void Reader_AfterReadData(object sender, ref DataAfterTransportEventArgs e)
        {
            if (e.IsSucceed)
            {
                DataPackageCollection col = e.OperateData;
                if (col.OperatedPkgCount == col.Count)
                {
                    Operator.OperateQueue.Enqueue(col);

                    if (Reader.GetTagLeftPacket(col.Tag) != -1)
                    {
                        return;
                    }
                    Listener.AddConnect(col.Tag);
                }
            }
        }

        #endregion

        #region========公共方法========

        /// <summary>
        /// 开启数据交换中心
        /// </summary>
        /// <returns></returns>
        public virtual bool Start()
        {
            Reader.StartListen();
            Reader.AfterReadData += Reader_AfterReadData;
            Sender.StartListen();
            Operator.StartListen();
            Operator.AfterOperateData += Operator_AfterOperateData;
            ConnectionManager.StartListen();
            ConnectionManager.NewConnectionJoin += ConnectionManager_NewConnectionJoin;
            Listener.Start();
            Listener.DataAvailable += DataListener_DataAvailable;
            return true;
        }


        /// <summary>
        /// 停止数据交换中心
        /// </summary>
        /// <returns></returns>
        public virtual bool Stop()
        {
            Reader.Stop();
            Reader.AfterReadData -= Reader_AfterReadData;
            Sender.Stop();
            Operator.Stop();
            Operator.AfterOperateData -= Operator_AfterOperateData;
            ConnectionManager.Stop();
            Listener.Stop();
            Listener.DataAvailable -= DataListener_DataAvailable;
            return true;
        }

        #endregion

    }
}
